package cn.edu.nuc.onlinestore.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import cn.edu.nuc.onlinestore.vo.Person;

public class Checkadmin implements Checked {
	private String username;
	private String password;
	
	public Checkadmin(){}
	@Override
	public boolean checkuser(String username, String password) throws IOException, ClassNotFoundException {
		this.password=password;
		this.username=username;
		
		FileInputStream in=new FileInputStream("D:/store/admin/"+username+".db");
		ObjectInputStream din=new ObjectInputStream(in);
		
		Person p=(Person)din.readObject();
		
		in.close();
		din.close();
		if(p.getUsername().equals(username)&&p.getPassword().equals(password)){
			return true;
		}else{
			return false;
		}
		
		// TODO Auto-generated method stub
		
	}

	
	@Override
	public boolean adduser(String username, String password) throws IOException, ClassNotFoundException {
	
	
		File f=new File("D:/store/admin/"+username+".db");
		if(f.exists()){
			return false;
		}else{
			FileOutputStream out=new FileOutputStream(f);
			ObjectOutputStream dout=new ObjectOutputStream(out);
			
			dout.writeObject(new Person(username,password));
			return true;
		}
	
	
	}

}
